home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / Little Smalltalk v3.1.4 / Smalltalk Source / browser.st < prev    next >
Encoding:
Text File  |  1995-01-24  |  7.6 KB  |  285 lines  |  [TEXT/KAHL]

  1. * ***
  2. * Methods for a class browser
  3. *
  4. * Julian Barkway (c) September 1994 All rights reserved. 
  5. *
  6. * v3.1.0    Initial release
  7. * v3.1.1    -
  8. * v3.1.2    - Changed to allow for different actions for double and single clicks.
  9. *            - Increased use of cascades.
  10. *            - addSubClass:instanceVariableNames: patched to let through embedded
  11. *              digits.
  12. * v3.1.3    - Dictionary pane no longer gives error if clicked on when empty.
  13. *
  14. * ***
  15. Class    BrowserWindow Window browser
  16. Class    DictionaryPane SelectListPane dict
  17. Class    Browser Object theClass method compile classPMenu methodPMenu textAreaPMenu cp mp tp
  18.  
  19. *
  20. * addSubClass:... patched so that it doesn't remove numbers embedded in instance 
  21. * variable names.  The downside is that it will let through some illegal names, but 
  22. * since the compiler will trap these when they are used it probably isn't too bad a
  23. * side-effect.
  24. *
  25. Methods Class 'creation'
  26.     addSubClass: aSymbol instanceVariableNames: aString    | newClass |
  27.         newClass <- Class new; name: aSymbol; superClass: self;
  28.                 variables: 
  29.                   (aString words: [:x | x isAlphaNumeric ]).
  30.         aSymbol assign: newClass.
  31.         classes at: aSymbol put: newClass
  32. ]
  33.  
  34. *
  35. * We sub-class Window to allow the 'close' message to be neatly trapped without
  36. * needing to make Browser a sub-class of Window (which it clearly isn't)
  37. Methods BrowserWindow 'all'
  38.     openAt: aPosition withSize: aSize
  39.         (title = '') ifTrue: [       " Only false when restoring a saveImage'd window "
  40.             self title: 'Browser ' , (nextBrowserNum asString).
  41.             nextBrowserNum <- nextBrowserNum + 1
  42.         ].
  43.         super openAt: aPosition withSize: aSize
  44. |
  45.     close    | reply |
  46.         (self wantsSave) ifTrue: [
  47.             reply <- smalltalk inquire: ('Save contents of window ', self title, '?').
  48.             (reply isNil) ifTrue: [                "Note: can return nil for Cancel"
  49.                 ^ nil
  50.             ]. 
  51.             reply  ifTrue: [
  52.                 ((self mainPane) saveContents: 1) ifFalse: [
  53.                     ^ nil            "User cancelled save operation"
  54.                 ]
  55.             ]
  56.         ].
  57.         super close.
  58.         browser close
  59. |
  60.     browser: aBrowser
  61.         browser <- aBrowser.
  62. ]
  63.  
  64. Methods DictionaryPane 'all'
  65.     dictionary: aDictionary
  66.         dict <- aDictionary.
  67. |
  68.     dictionary
  69.         ^ dict
  70. |
  71.     setText | t |
  72.         t <- '------------------' , newLine.
  73.         dict binaryDo: [:a :b |
  74.             t <- t , (a asString) , newLine
  75.         ].
  76.         t <- t , '------------------'.
  77.         self clearAllText.
  78.         self text: t
  79. |
  80.     getSelectedItem | t |            " Changed for v3.1.3 "
  81.         dict isNil ifTrue: [
  82.             ^ nil
  83.         ].
  84.         t <- self selectedText.
  85.          ( (t = '------------------') or:
  86.            [t = ('------------------' , newLine) ]) ifTrue: [
  87.             ^ nil
  88.         ].
  89.         t <- t copyFrom: 1 to: ((t size) - 1).
  90.         dict binaryDo: [:a :b |
  91.             ((t asSymbol) == a) ifTrue: [
  92.                 ^ b 
  93.             ]
  94.         ]
  95. ]
  96.  
  97. Methods Browser 'all'
  98.     new | bwin maxW maxH | 
  99.         maxW <- (smalltalk getMaxScreenArea) right.
  100.         maxW <- 450 min: (maxW - 70).
  101.         maxH <- (smalltalk getMaxScreenArea) bottom.
  102.         maxH <- 500 min: (maxH - 70).
  103.         bwin <- BrowserWindow new; 
  104.             browser: self;
  105.             openAt: (20@60) withSize: (maxW@maxH).
  106.         self makeClassPopUpMenu.
  107.         self makeMethodPopUpMenu.
  108.         self makeTextAreaPopUpMenu.
  109.         self makePanes: bwin.
  110. |
  111.     makePanes: bwin        | ww wh ph pw |
  112.         ww <- (bwin size) x.
  113.         wh <- (bwin size) y.
  114.         pw <- (ww / 2) truncated.
  115.         ph <- (wh / 5) truncated.
  116.         cp <- DictionaryPane new;
  117.             dictionary: classes;
  118.             boundsFrom: (-1 @ -1) to: (pw @ ph);
  119.             attachTo: bwin withSizing: (0 @ 0).
  120.         mp <- DictionaryPane new;
  121.             boundsFrom: ((pw - 1) @ -1) to: ((ww + 1) @ ph);
  122.             attachTo: bwin withSizing: (1 @ 0).
  123.         tp <- TextPane new;
  124.             boundsFrom: (-1 @ (ph - 1)) to: ((ww + 1) @ (wh + 1));
  125.             attachTo: bwin withSizing: (1 @ 1).
  126. |
  127.     open
  128.         cp font: 'geneva'; fontSize: 9; typeFace: 2;
  129.             button1Action: [:p | 
  130.                 mp clearAllText. 
  131.                 classPMenu enableItem: 1; enableItem: 2; enableItem: 3.
  132.                 "classPMenu enableItem: 4." "Class removal disabled for now..."
  133.                 methodPMenu disableItem: 1; disableItem: 2.
  134.                 self cancel
  135.             ];
  136.             button2Action: [:p | classPMenu popUpAt: p ];
  137.             button1DoubleClick: [:p | self selectClass: (cp getSelectedItem) ].
  138.         mp font: 'geneva'; fontSize: 9; typeFace: 2;
  139.             button1Action: [:p | 
  140.                 self cancel.
  141.                 methodPMenu enableItem: 2
  142.             ];
  143.             button2Action: [:p | methodPMenu popUpAt: p ];
  144.             button1DoubleClick: [:p | self selectMethod: (mp getSelectedItem) ].
  145.         tp font: 'monaco'; fontSize: 9;
  146.             button2Action: [:p | textAreaPMenu popUpAt: p ].
  147.         cp setText
  148. |
  149.     close
  150.         " close our window and remove the pop-up menus"
  151.         classPMenu dispose.
  152.         methodPMenu dispose.
  153.         textAreaPMenu dispose
  154. |
  155.     selectClass: c
  156.         (c notNil) ifTrue: [
  157.             theClass <- c.
  158.             self showMethods
  159.         ].
  160.         methodPMenu enableItem: 1; disableItem: 2.
  161. |
  162.     showMethods
  163.         mp dictionary: (theClass methods).
  164.         mp setText
  165. |
  166.     selectMethod: m
  167.         (m notNil) ifTrue: [
  168.             method <- m.
  169.             tp print: m text.
  170.             compile <- true
  171.         ].
  172.         textAreaPMenu enableItem: 1; enableItem: 2
  173. |
  174.     makeClassPopUpMenu
  175.         classPMenu <- PopUpMenu new; 
  176.             title: '';
  177.             create.
  178.         classPMenu addItem: 'Browse Class' 
  179.                         action: [ self browseClass: (cp getSelectedItem) ];
  180.                    addItem: 'Inspect Class' 
  181.                         action: [ (cp getSelectedItem) inspect ];
  182.                    addItem: 'Add New Class'
  183.                         action: [ self addClass ];
  184.                    addItem: 'Remove Class'
  185.                         action: [ self removeClass: (cp getSelectedItem) ];
  186.                    disableItem: 1; disableItem: 4.
  187. |
  188.     makeMethodPopUpMenu
  189.         methodPMenu <- PopUpMenu new; 
  190.             title: '';
  191.             create.
  192.         methodPMenu addItem: 'Add New Method'
  193.                         action: [ self addMethod ];
  194.                     addItem: 'Remove Method'
  195.                         action: [ self removeMethod: (mp getSelectedItem) ];
  196.                     disableItem: 1; disableItem: 2.
  197. |
  198.     makeTextAreaPopUpMenu
  199.         textAreaPMenu <- PopUpMenu new; 
  200.             title: '';
  201.             create.
  202.         textAreaPMenu addItem: 'Accept' action: [ self accept ];
  203.                       addItem: 'Cancel' action: [ self cancel ];
  204.                       disableItem: 1;
  205.                       disableItem: 2.
  206.         compile <- true
  207. |
  208.     browseClass: c    | iv |
  209.         " browse the given class "
  210.         (c notNil) ifTrue: [
  211.             tp clearAllText;
  212.                print: (c superClass) printString , ' addSubClass: #' , 
  213.                         c printString, newLine;
  214.                print: '   instanceVariableNames: '''.
  215.             iv <- c variables.
  216.             (iv notNil) ifTrue: [
  217.                 iv do: [ :var | tp print: ((var asString) , ' ') ]
  218.             ].
  219.             tp print: ''''.
  220.             theClass <- c.
  221.             compile <- false
  222.         ].
  223.         textAreaPMenu disableItem: 1; enableItem: 2.
  224. |
  225.     addClass    
  226.         " add a new class "
  227.         tp clearAllText;
  228.            print: 'superClass addSubClass: #nameOfClass ', newLine,
  229.                    '  instanceVariableNames: ''var1 var2'' '.
  230.         compile <- false.
  231.         textAreaPMenu enableItem: 1; enableItem: 2.
  232. |
  233.     removeClass: c    
  234.         " Will remove class from symbols dictionary
  235.           when we can figure out how... It's not just 
  236.           a simple matter of invoking removeKey: "
  237.         ^ nil
  238. |
  239.     addMethod
  240.         method <- Method new.
  241.         tp clearAllText.
  242.         tp print: '  "A comment stating the method''s function"' , newLine.
  243.         tp print: '  messageSelector: argumentNames " -- argument names optional"', newLine.
  244.         tp print: '    | temporaries | "-- temporaries optional"', newLine.
  245.         tp print: '    body of method', newLine.
  246.         compile <- true.
  247.         textAreaPMenu enableItem: 1.
  248.         textAreaPMenu enableItem: 2.
  249. |
  250.     removeMethod: m | t |
  251.         " Remove given method from currently selected class "
  252.         (m notNil) ifTrue: [
  253.             t <- (smalltalk inquire: 'Remove method ''', (m name), '''?').
  254.             (t isNil) ifTrue: [
  255.                 ^ nil
  256.             ].
  257.             t ifTrue: [
  258.                 (mp dictionary) removeKey: (m name).
  259.                 mp setText.
  260.                 methodPMenu disableItem: 2
  261.             ]
  262.         ]
  263. |
  264.     accept
  265.         compile ifTrue:  [ self compile   ]
  266.                 ifFalse: [ self doCommand ]
  267. |
  268.     cancel
  269.         tp clearAllText.
  270.         textAreaPMenu disableItem: 1.
  271.         textAreaPMenu disableItem: 2.
  272.  
  273. |
  274.     compile
  275.         method text: (tp text).
  276.         (method compileWithClass: theClass)
  277.             ifTrue: [ theClass methods at: method name put: method.
  278.                 mp setText ].
  279. |
  280.     doCommand
  281.         " accept tw command "
  282.         [ tp text execute. cp setText ] fork.
  283. ]
  284.